﻿/* Serialize Plugin Context
 * Copyright Andrew Swerlick - Engage, Inc. 2010
 * is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version.
 * Serialize Plugin Context is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with Serialize Plugin Context. 
 * If not, see http://www.gnu.org/licenses/.
 ***********************************************/
using System;
using SerializePluginContext;
using Microsoft.Xrm.Sdk;

namespace Engage.Util.PluginTester
{
    /// <summary>
    /// A lightweight implementation of the IServiceProvider Interface for the purposes of testing the execute method of a CRM plug-in
    /// </summary>
    public class MyServiceProvider : IServiceProvider
    {

        public MyOrganizationServiceFactory serviceFactory;
        public MyPluginContext context;
        public ITracingService tracer;

        /// <summary>
        /// Default constructor. You must explicitly set the context and serviceFactory memebers for the GetService method to work
        /// </summary>
        public MyServiceProvider()
        {
        
        }

        /// <summary>
        /// Constructor that automatically handles creating the context. You must explicitly set the ServiceFactory Member to 
        /// connect to the CRM service from within the Execute Method of the plugin.
        /// </summary>
        /// <param name="filepath"></param>
        public MyServiceProvider(string filepath) : this (null, null, null, filepath)
        {
            
        }

        /// <summary>
        /// Constructor that automatically handles creating the serviceFactory member. You must explicitly set the context member
        /// to use the IPluginContextExecution object from within the Execute Method of the plugin.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pass"></param>
        /// <param name="servername"></param>
        public MyServiceProvider(string username, string pass, string servername): this (username, pass, servername, null)
        {

        }

        /// <summary>
        /// Constructor that automatically handles creating the context and the serviceFactory
        /// </summary>
        /// <param name="username">The username used to connect to the crm service</param>
        /// <param name="password">The password to connect to the crm service</param>
        /// <param name="servername">The url to the Organization.svc service for the organization</paramm>
        /// <param name="filepath">The filepath to the serialized context object</param>
        public MyServiceProvider(string username, string pass, string servername, string filepath)
        {

            if (username != null)
            {
                MyOrganizationServiceFactory serviceFact = new MyOrganizationServiceFactory();
                serviceFact.UserName = username;
                serviceFact.Password = pass;
                serviceFact.ServerName = servername;
                serviceFact.SetService();
                this.serviceFactory = serviceFact;
            }

            tracer = new SerialPluginTracer();

           //Deserialize the context object found at the MyServiceProvider's file-path to create a context object for testing
           if (filepath != null) { MyPluginContext myPluginContext = PluginTester.DeSerializePluginContext(filepath); }
        }



        #region IServiceProvider Members

        /// <summary>
        /// The implementation of the GetService object. Simply returns the context, or serviceFactory properties depending on the value passed in.
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns>Either the plugin context or service factory</returns>
        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(IPluginExecutionContext))
            {
                return this.context;
            }
            else if (serviceType  == typeof(IOrganizationServiceFactory))
            {

                return serviceFactory;
            }
            else if (serviceType == typeof(ITracingService))
            {
                return tracer;
            }
            
            return null;

        }

        #endregion
    }
}
